import { cloneDeep, isEmpty } from '@src/util/type';
/* types */
import { AIChatRobotItemType } from "@src/component/business/BizChatPanel/chat/types";
import { 
  AIChatRobotAppItemComponentType,
  AIChatRobotItemComponentType, 
  SettingGPTRobotAppComponentItem, 
  SettingGPTRobotAppConfigType, 
  SettingGPTRobotAppItem, 
  SettingGPTRobotModuleConfigType, 
  SettingGPTRobotModuleType, 
  SettingGPTRobotOutsideModuleAppGroupType, 
  SettingGPTRobotOutsideModuleAppType, 
  SettingGPTServiceItem
} from "@src/modules/setting/gpt/types";
/* model */
import { 
  DefaultRobotAppItem, 
  DefaultRobotItem, 
  DefaultRobotServiceTypeList, 
  RobotServiceList, 
  SettingGPTServiceTypeEnum, 
  SettingGPTServiceTypeSettingEnum 
} from "@src/modules/setting/gpt/model";
/* util */
import { isFalsy } from '@src/util/type';
import { parse_with_default_value } from "@src/util/lang/object";
import { openTabGptRobotAppIntelligentQA, openTabGptRobotAppBI, openTabGptRobotAppTranslate } from '@src/util/business/openTab';

function transformRobotServerItemToComponentItem(robotServerItem: AIChatRobotItemType): AIChatRobotItemComponentType {
  
  const defaultSetting = DefaultRobotItem.setting
  
  const setting = parse_with_default_value(robotServerItem.setting, defaultSetting)
  
  return {
    ...robotServerItem,
    setting
  }
}

function transformRobotAppServerItemToComponentItem(robotAppServerItem: SettingGPTRobotAppItem): AIChatRobotAppItemComponentType {
  
  const defaultSetting = DefaultRobotAppItem.setting
  const setting = parse_with_default_value(robotAppServerItem.setting, defaultSetting)
  
  if (isFalsy(setting.promptWordList) || isEmpty(setting.promptWordList)) {
    setting.promptWordList = defaultSetting.promptWordList
  }
  
  return {
    ...robotAppServerItem,
    setting
  }
}

function getRobotOutsideModulesByModuleGroups(moduleGroups: SettingGPTRobotOutsideModuleAppGroupType[]): SettingGPTRobotOutsideModuleAppType[] {
  return moduleGroups.reduce((prev, curr) => {
    return prev.concat(curr.appList)
  }, [] as SettingGPTRobotOutsideModuleAppType[])
}

function transformRobotOutsideAppToOutsideModule(apps: SettingGPTRobotOutsideModuleAppType[]): SettingGPTRobotModuleType[] {
  return apps.map(app => {
    return {
      name: app.appName,
      value: app.appId,
      show: false,
      disabled: true,
      enable: false
    }
  })
}

function isRobotSystem(robot: Record<string, any>): boolean {
  return robot.robotNo == '1'
}

function isRobotEnabled(robot: Record<string, any>): boolean {
  return Boolean(robot.enable)
}

function getRobotServiceByModuleConfig(moduleConfig?: SettingGPTRobotModuleConfigType): SettingGPTServiceItem[] {
  
  if (isFalsy(moduleConfig)) {
    return RobotServiceList.filter(service => DefaultRobotServiceTypeList.includes(service.type))
  }
  
  // 获取启用的模块
  const enabledModules = moduleConfig!.modules.filter(module => module.enable)
  // 获取启用的服务
  const serviceList = RobotServiceList.filter(service => {
    return enabledModules.some(module => module.value == service.type)
  })
  // 返回服务列表
  return serviceList
}

function transformRobotAppToComponentRobotItem(appList: SettingGPTRobotAppItem[]): SettingGPTServiceItem[] {
  
  return appList.map(app => {
    
    const setting = SettingGPTServiceTypeSettingEnum[app.type] || {} as SettingGPTRobotAppConfigType
    
    return {
      ...app,
      type: app.type,
      name: setting.name,
      icon: setting.icon,
      description: setting.description,
      descriptionBottom: setting.descriptionBottom,
      disabled: false,
      show: true,
      hasUrl: setting.hasUrl,
      system: setting.system,
      setting: parse_with_default_value(app.setting, {} as SettingGPTRobotAppConfigType)
    }
    
  })
}

function openRobotAppPage(item: SettingGPTServiceItem, robotId: string) {
  
  const type = item.type
  
  if (type == SettingGPTServiceTypeEnum.Intelligent) {
    return openTabGptRobotAppIntelligentQA(item.id, robotId)
  }
  
  if (type == SettingGPTServiceTypeEnum.BI) {
    return openTabGptRobotAppBI(item.id, robotId)
  }
  
  if (type == SettingGPTServiceTypeEnum.ChineseToEnglish) {
    return openTabGptRobotAppTranslate(item.id, robotId)
  }
  
  throw new Error(`未知的机器人服务类型: ${type}`)
}

export {
  transformRobotServerItemToComponentItem,
  getRobotOutsideModulesByModuleGroups,
  transformRobotOutsideAppToOutsideModule,
  isRobotSystem,
  getRobotServiceByModuleConfig,
  isRobotEnabled,
  transformRobotAppToComponentRobotItem,
  transformRobotAppServerItemToComponentItem,
  openRobotAppPage
}